package com.ruoyi.ws.facade;


import java.util.List;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.shiro.service.SysPasswordService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.responseDto.UserDto;
import com.ruoyi.system.service.IAdminBookService;
import com.ruoyi.system.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import static com.ruoyi.common.core.domain.AjaxResult.error;
import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 路径规划显示配置规则REST服务：提供有关路径规划显示配置规则的接口
 *
 * @author roytrack
 * @date 2020-05-25 17:37:24
 */
@RestController
@RequestMapping(path = "services/chargeConfig")
public class ChargeConfigResource {

  /**
   * LOGGER
   */
  private static final Logger LOGGER = LoggerFactory.getLogger(ChargeConfigResource.class);
  @Autowired
  private IAdminBookService adminBookService;
  @Autowired
  private SysPasswordService passwordService;
  @Autowired
  private ISysUserService userService;

  @PostMapping("/login")
  @ResponseBody
  public AjaxResult login (String loginName, String password) {
    if (StringUtils.isEmpty(loginName) || StringUtils.isEmpty(password)) {
      return error("用户或密码不能为空");
    }
    try {
      SysUser sysUser = new SysUser();
      sysUser.setLoginName(loginName);
      List<SysUser> userList = userService.selectUserList(sysUser);
      if (null != userList && userList.size() > 0) {
        String passwordMd5 = passwordService.encryptPassword(loginName, password, userList.get(0).getSalt());
        if (userList.get(0).getPassword().equals(passwordMd5)) {
          UserDto user = new UserDto();
          user.setUserId(userList.get(0).getUserId());
          user.setUserName(userList.get(0).getUserName());
          user.setUserType(userList.get(0).getUserType());
          user.setLoginName(userList.get(0).getLoginName());
          //更新登陆标识
          sysUser.setLoginFlag("1");
          sysUser.setUserId(userList.get(0).getUserId());
          userService.updateUser(sysUser);
          return success(user);
        } else {
          return error("密码错误");
        }
      } else {
        return error("用户不存在");
      }
    } catch (Exception e) {
      return error("用户或密码错误");
    }
  }

  @PostMapping("/loginout")
  @ResponseBody
  public AjaxResult loginout (Long userId) {
    if (null==userId) {
      return error("用户id不能为空！");
    }
    try {
      SysUser sysUser = new SysUser();
      sysUser.setUserId(userId);
      sysUser.setLoginFlag("0");
      int result = userService.updateUser(sysUser);
      if (result > 0) {
          return success();
      } else {
        return error("登出失败！");
      }
    } catch (Exception e) {
      return error("系统错误");
    }
  }

}
